﻿
// MFCApplication1View.cpp: CMFCApplication1View 类的实现
//

#include "pch.h"
#include "framework.h"
// SHARED_HANDLERS 可以在实现预览、缩略图和搜索筛选器句柄的
// ATL 项目中进行定义，并允许与该项目共享文档代码。
#ifndef SHARED_HANDLERS
#include "MFCApplication1.h"
#endif

#include "MFCApplication1Doc.h"
#include "MFCApplication1View.h"
#include "MyDialog.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CMFCApplication1View

IMPLEMENT_DYNCREATE(CMFCApplication1View, CView)

BEGIN_MESSAGE_MAP(CMFCApplication1View, CView)
	// 标准打印命令
	ON_COMMAND(ID_FILE_PRINT, &CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, &CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, &CView::OnFilePrintPreview)
	ON_WM_RBUTTONDOWN()
//	ON_COMMAND(ID_POPUP_LINE, &CMFCApplication1View::OnPopupLine)
//	ON_COMMAND(ID_POPUP_ELLIPSE, &CMFCApplication1View::OnPopupEllipse)
//	ON_COMMAND(ID_POPUP_RECTANGLE, &CMFCApplication1View::OnPopupRectangle)
//	ON_UPDATE_COMMAND_UI(ID_POPUP_ELLIPSE, &CMFCApplication1View::OnUpdatePopupEllipse)
ON_COMMAND(ID_POPUP_ELLIPSE, &CMFCApplication1View::OnPopupEllipse)
ON_COMMAND(ID_POPUP_RECTANGLE, &CMFCApplication1View::OnPopupRectangle)
ON_WM_LBUTTONDOWN()

ON_COMMAND(ID_UPDATE, &CMFCApplication1View::OnUpdate)
END_MESSAGE_MAP()

// CMFCApplication1View 构造/析构

CMFCApplication1View::CMFCApplication1View() noexcept
{
	// TODO: 在此处添加构造代码

}

CMFCApplication1View::~CMFCApplication1View()
{
}

BOOL CMFCApplication1View::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: 在此处通过修改
	//  CREATESTRUCT cs 来修改窗口类或样式

	return CView::PreCreateWindow(cs);
}

// CMFCApplication1View 绘图

void CMFCApplication1View::OnDraw(CDC* pDC)
{
	CMFCApplication1Doc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	if (!pDoc)
		return;
	//清除界面已有内容
	OnUpdate();
	//字符串保存图形的类型、左上角坐标、宽度和高度
	CString str;
	str.Format(L"type = %s, x = %d, y = %d, width = %d, height = %d", pDoc->type, pDoc->m_x, pDoc->m_y, pDoc->width, pDoc->height);
	//根据类型画图像
	//文本输出坐标根据图像的显示位置进行调整，保持在图形正下方
	if (pDoc->type == "Rectangle") {
		pDC->Rectangle(pDoc->m_x, pDoc->m_y, pDoc->m_x + pDoc->width, pDoc->m_y + pDoc->height);
		pDC->TextOutW(pDoc->m_x - pDoc->width / 2, pDoc->m_y + pDoc->height + 50, str);
	}
	else if (pDoc->type == "Ellipse") {
		pDC->Ellipse(pDoc->m_x, pDoc->m_y, pDoc->m_x + pDoc->width, pDoc->m_y + pDoc->height);
		pDC->TextOutW(pDoc->m_x - pDoc->width / 2, pDoc->m_y + pDoc->height + 50, str);
	}
}


// CMFCApplication1View 打印

BOOL CMFCApplication1View::OnPreparePrinting(CPrintInfo* pInfo)
{
	// 默认准备
	return DoPreparePrinting(pInfo);
}

void CMFCApplication1View::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: 添加额外的打印前进行的初始化过程
}

void CMFCApplication1View::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: 添加打印后进行的清理过程
}


// CMFCApplication1View 诊断

#ifdef _DEBUG
void CMFCApplication1View::AssertValid() const
{
	CView::AssertValid();
}

void CMFCApplication1View::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CMFCApplication1Doc* CMFCApplication1View::GetDocument() const // 非调试版本是内联的
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMFCApplication1Doc)));
	return (CMFCApplication1Doc*)m_pDocument;
}
#endif //_DEBUG


// CMFCApplication1View 消息处理程序


void CMFCApplication1View::OnRButtonDown(UINT nFlags, CPoint point)
{
	//右键消息，浮动菜单
	CMenu menu;
	menu.CreatePopupMenu();
	//两个可选菜单项
	menu.AppendMenuW(MF_ENABLED, ID_POPUP_ELLIPSE, L"Ellipse");
	menu.AppendMenuW(MF_ENABLED, ID_POPUP_RECTANGLE, L"Rectangle");
	CPoint pt;
	//获取光标位置
	GetCursorPos(&pt);
	//弹出菜单
	menu.TrackPopupMenu(TPM_LEFTALIGN,pt.x,pt.y,this);
	CView::OnRButtonDown(nFlags, point);
}





//菜单点击消息
void CMFCApplication1View::OnPopupEllipse()
{
	//获取设备环境
	CDC* pDC = GetDC();
	CRect rect;
	//获取客户区信息
	GetClientRect(&rect);
	//用笔刷填充白色，刷新
	CBrush brush(RGB(255, 255, 255));
	pDC->FillRect(&rect, &brush);
	CMFCApplication1Doc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	if (!pDoc)
		return;
	//根据客户区大小得到中心位置，将属性保存至document
	pDoc->m_x = rect.right / 2 - 100;
	pDoc->m_y = rect.bottom / 2 - 100;
	pDoc->width = 200;
	pDoc->height = 200;
	pDoc->type = "Ellipse";
	pDC->Ellipse(pDoc->m_x, pDoc->m_y, pDoc->m_x +200, pDoc->m_y +200);
	//需要发送WM_PAINT消息，否则不能在客户区及时更新图形的属性信息
	Invalidate();
}

//菜单点击消息，基本内容同上
void CMFCApplication1View::OnPopupRectangle()
{
	CDC* pDC = GetDC();
	CRect rect;
	GetClientRect(&rect);
	CBrush brush(RGB(255, 255, 255));
	pDC->FillRect(&rect, &brush);
	CMFCApplication1Doc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	if (!pDoc)
		return;
	pDoc->m_x = rect.right / 2 - 100;
	pDoc->m_y = rect.bottom / 2 - 100;
	pDoc->width = 200;
	pDoc->height = 200;
	pDoc->type = "Rectangle";
	pDC->Rectangle(pDoc->m_x, pDoc->m_y, pDoc->m_x + 200, pDoc->m_y + 200);
	Invalidate();
}



//刷新客户区
void CMFCApplication1View::OnUpdate()
{
	//获取设备环境
	CDC* pDC = GetDC();
	CRect rect;
	//获取客户区
	GetClientRect(&rect);
	CBrush brush(RGB(255, 255, 255));
	//笔刷填充白色
	pDC->FillRect(&rect, &brush);
}
